package com.moon.game.manager;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.moon.game.constants.Constants;
import com.moon.game.resource.model.Monster;
import com.moon.game.util.Utils;

/**
 * @description 怪物管理
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-4-29 下午3:30:31
 */
public class MonsterMG {
	private static final Logger log = Logger.getLogger(MonsterMG.class);
	public static ConcurrentHashMap<Integer, ArrayList<Monster>> monsterByLevel = new ConcurrentHashMap<Integer, ArrayList<Monster>>();
	public static ConcurrentHashMap<Integer, Monster> monsterById = new ConcurrentHashMap<Integer, Monster>();
	public static ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, ArrayList<Monster>>> monsterByPropertyAndLevel = new ConcurrentHashMap<Integer, ConcurrentHashMap<Integer,ArrayList<Monster>>>();
	private MonsterMG() {
	}
	public static MonsterMG instance = new MonsterMG();
	public void init() {
		log.debug("load monsters start...");
		loadMonsters();
		log.debug("load monsters end...");
	}
	
	/**
	 * 加载所有怪物
	 * 
	 */
	private void loadMonsters() {
		List<Monster> monsters = DBManager.queryAllMonsters();
		for (Monster monster : monsters) {
			//20级划分怪物
			int monsterLevel = monster.getLevel() / 20 + 1;
			if (monsterByLevel.get(monsterLevel) == null) {
				ArrayList<Monster> monsterList = new ArrayList<Monster>();
				monsterList.add(monster);
				monsterByLevel.put(monsterLevel, monsterList);
			}else {
				monsterByLevel.get(monsterLevel).add(monster);
			}
			
			//属性，等级不同的怪物加载到内存
			ConcurrentHashMap<Integer, ArrayList<Monster>> monsterForLevel = null;
			if (monsterByPropertyAndLevel.get(monster.getProperty()) == null) {
				monsterForLevel = new ConcurrentHashMap<Integer, ArrayList<Monster>>();
			}else {
				monsterForLevel = monsterByPropertyAndLevel.get(monster.getProperty());
			}
			if (monsterForLevel.get(monsterLevel) == null) {
				ArrayList<Monster> monsterList = new ArrayList<Monster>();
				monsterList.add(monster);
				monsterForLevel.put(monsterLevel, monsterList);
			}else {
				monsterForLevel.get(monsterLevel).add(monster);
			}
			monsterByPropertyAndLevel.put(monster.getProperty(), monsterForLevel);
			
			//按照id存放怪物
			monsterById.put(monster.getId(), monster);
		}
	}
	
	/**
	 * 根据怪物id寻找怪物
	 * 
	 * @param id
	 * @return
	 */
	public final Monster getMonsterById(int id) {
		return monsterById.get(id);
	}

	/**
	 * 根据等级寻找怪物列表
	 * 
	 * @param level
	 * @return
	 */
	public final ArrayList<Monster> getMonsterByLeve(int level) {
		return monsterByLevel.get(level);
	}
	
	/**
	 * 根据属性和等级获取怪物列表
	 * 
	 * @param property
	 * @param level
	 * @return
	 */
	public final ArrayList<Monster> getMonsterByPropertyAndLevel(int property, int level) {
		return monsterByPropertyAndLevel.get(property).get(level);
	}
	
	/**
	 * 怪物工厂
	 * 
	 * @param id
	 * @return
	 */
	public Monster monsterFactory(int id) {
		Monster newMonster = new Monster();
		Monster resMonster = getMonsterById(id);
		Utils.copyProperties(newMonster, resMonster);
		String uuid = UUID.randomUUID().toString();
		newMonster.setMonsterId(uuid);
		String dropItem = Utils.calculateProbability(resMonster.getDropItemProb(), Constants.MONSTER_DROP_BASE, resMonster.getDropTimes());
		newMonster.setDropItemProb(dropItem);
		int proMoney = new Random().nextInt(Constants.MONEY_DROP_BASE) + 1;
		newMonster.setMoney(proMoney > resMonster.getMoney() ? 0 : proMoney);
		return newMonster;
	}
}
